
package system.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.ResultSet;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.hitrust.gs.lib.GSAutoAuditongDateConfigurator;

import framework.core.*;
import system.LoginContext;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-02-19 13:17:40
*/
public class ProductStorageSrv extends IHandler
{
	private boolean isTel=false;
	public ProductStorageSrv() 
	{
		super();
	}
	private void deleteProductStorage(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		ProductStorage obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = ProductStorage.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveProductStorage(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		ProductStorage obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = ProductStorage.fetch();
    	}
    	else                     //update
    	{
    		obj = ProductStorage.fetch(con,id);
    	}
		ProductStoragePageData data = (ProductStoragePageData)PageData.fromForm(context,ProductStoragePageData.class);
		context.setValue("ProductStoragePageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listProductStorage(DBConnection con,SrvContext context,ILoginContext loginContext,String path) throws Exception
	{
	    boolean isFirst = context.getBoolean("isFirst");
		WarehouseProductViewSearchEvent event = (WarehouseProductViewSearchEvent)SearchEvent.fromForm(context,WarehouseProductViewSearchEvent.class);
        String []warehouseCodes = null;
        String[] warehouseCodesSplit;
        String warehouseCodesService ="";
        String[] warehouseTreeCodes ;
        String isFirstVisit = "false";
        if (isFirst)
        {
        	isFirstVisit = "true";
        	warehouseTreeCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
            warehouseCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
        }
        else
        {
            warehouseCodes = context.getParameterValues("WarehouseCodes");
            if (warehouseCodes == null||warehouseCodes.length==0||warehouseCodes[0].equals(""))
            {
                warehouseCodes = new String[]{"--1"};
                warehouseTreeCodes = new String[]{"--1"};
            }else{
            	warehouseCodesSplit = warehouseCodes[0].split(",");
            	for (int j = 0; j < warehouseCodesSplit.length; j++) {
	        		String warehouseObjectListService = Warehouse.fetchWarehousesForString(con, warehouseCodesSplit[j]);
    				String[] service = warehouseObjectListService.split(",");
    				for (int i = 0; i < service.length; i++) {
    					warehouseCodesService += service[i]+",";
    				}
				}
            	warehouseCodes = warehouseCodesService.split(",");
            	warehouseTreeCodes =  context.getParameterValues("WarehouseCodes");
            }
            isFirstVisit = "false";
        }
        event.setWarehouseTreeCodes(warehouseTreeCodes);
		event.setWarehouseCodes(warehouseCodes);
		event.setWarehouseCodeUser(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode()));
		context.setSessionValue("WarehouseProductViewSearchEvent",event);
		context.setNextUrl(path+"ProductStorageList.jsp"+"?isFirstVisit="+isFirstVisit);
	}
	private void listProductStorageShowNowQuery(DBConnection con,SrvContext context,ILoginContext loginContext,String path) throws Exception
	{
	    boolean isFirst = context.getBoolean("isFirst");
		ProductStorageSearchEvent event = (ProductStorageSearchEvent)SearchEvent.fromForm(context,ProductStorageSearchEvent.class);
        String []warehouseCodes = null;
        String[] warehouseCodesSplit;
        String warehouseCodesService ="";
        String[] warehouseTreeCodes ;
        String isFirstVisit = "false";
        if (isFirst)
        {
        	isFirstVisit = "true";
        	warehouseTreeCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
            warehouseCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
        }
        else
        {
            warehouseCodes = context.getParameterValues("WarehouseCodes");
            if (warehouseCodes == null||warehouseCodes.length==0||warehouseCodes[0].equals(""))
            {
                warehouseCodes = new String[]{"--1"};
                warehouseTreeCodes = new String[]{"--1"};
            }else{
            	warehouseCodesSplit = warehouseCodes[0].split(",");
            	for (int j = 0; j < warehouseCodesSplit.length; j++) {
	        		String warehouseObjectListService = Warehouse.fetchWarehousesForString(con, warehouseCodesSplit[j]);
    				String[] service = warehouseObjectListService.split(",");
    				for (int i = 0; i < service.length; i++) {
    					warehouseCodesService += service[i]+",";
    				}
				}
            	warehouseCodes = warehouseCodesService.split(",");
            	warehouseTreeCodes =  context.getParameterValues("WarehouseCodes");
            }
            isFirstVisit = "false";
        }
        event.setIsShow("1");
        event.setWarehouseTreeCodes(warehouseTreeCodes);
		event.setWarehouseCodes(warehouseCodes);
		event.setWarehouseCodeUser(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode()));
		context.setSessionValue("WarehouseProductViewSearchEvent",event);
		context.setNextUrl(path+"ProductStorageShowQueryList.jsp"+"?isFirstVisit="+isFirstVisit);
	}
	private void listProductStorageShowNow(DBConnection con,SrvContext context,ILoginContext loginContext,String path) throws Exception
	{
	    boolean isFirst = context.getBoolean("isFirst");
		ProductStorageSearchEvent event = (ProductStorageSearchEvent)SearchEvent.fromForm(context,ProductStorageSearchEvent.class);
        String []warehouseCodes = null;
        String[] warehouseCodesSplit;
        String warehouseCodesService ="";
        String[] warehouseTreeCodes ;
        String isFirstVisit = "false";
        if (isFirst)
        {
        	isFirstVisit = "true";
        	warehouseTreeCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
            warehouseCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
        }
        else
        {
            warehouseCodes = context.getParameterValues("WarehouseCodes");
            if (warehouseCodes == null||warehouseCodes.length==0||warehouseCodes[0].equals(""))
            {
                warehouseCodes = new String[]{"--1"};
                warehouseTreeCodes = new String[]{"--1"};
            }else{
            	warehouseCodesSplit = warehouseCodes[0].split(",");
            	for (int j = 0; j < warehouseCodesSplit.length; j++) {
	        		String warehouseObjectListService = Warehouse.fetchWarehousesForString(con, warehouseCodesSplit[j]);
    				String[] service = warehouseObjectListService.split(",");
    				for (int i = 0; i < service.length; i++) {
    					warehouseCodesService += service[i]+",";
    				}
				}
            	warehouseCodes = warehouseCodesService.split(",");
            	warehouseTreeCodes =  context.getParameterValues("WarehouseCodes");
            }
            isFirstVisit = "false";
        }
        event.setIsShow("1");
        event.setWarehouseTreeCodes(warehouseTreeCodes);
		event.setWarehouseCodes(warehouseCodes);
		event.setWarehouseCodeUser(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode()));
		context.setSessionValue("WarehouseProductViewSearchEvent",event);
		context.setNextUrl(path+"ProductStorageShowList.jsp"+"?isFirstVisit="+isFirstVisit);
	}
	private void listProductStorageNow(DBConnection con,SrvContext context,ILoginContext loginContext,String path) throws Exception
	{
	    boolean isFirst = context.getBoolean("isFirst");
		ProductStorageSearchEvent event = (ProductStorageSearchEvent)SearchEvent.fromForm(context,ProductStorageSearchEvent.class);
        String []warehouseCodes = null;
        String[] warehouseCodesSplit;
        String warehouseCodesService ="";
        String[] warehouseTreeCodes ;
        String isFirstVisit = "false";
        if (isFirst)
        {
        	isFirstVisit = "true";
        	warehouseTreeCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
            warehouseCodes = new String[]{loginContext.getLoginWarehouseCode()}; 
        }
        else
        {
            warehouseCodes = context.getParameterValues("WarehouseCodes");
            if (warehouseCodes == null||warehouseCodes.length==0||warehouseCodes[0].equals(""))
            {
                warehouseCodes = new String[]{"--1"};
                warehouseTreeCodes = new String[]{"--1"};
            }else{
            	warehouseCodesSplit = warehouseCodes[0].split(",");
            	for (int j = 0; j < warehouseCodesSplit.length; j++) {
	        		String warehouseObjectListService = Warehouse.fetchWarehousesForString(con, warehouseCodesSplit[j]);
    				String[] service = warehouseObjectListService.split(",");
    				for (int i = 0; i < service.length; i++) {
    					warehouseCodesService += service[i]+",";
    				}
				}
            	warehouseCodes = warehouseCodesService.split(",");
            	warehouseTreeCodes =  context.getParameterValues("WarehouseCodes");
            }
            isFirstVisit = "false";
        }
        event.setWarehouseTreeCodes(warehouseTreeCodes);
		event.setWarehouseCodes(warehouseCodes);
		event.setWarehouseCodeUser(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode()));
		context.setSessionValue("WarehouseProductViewSearchEvent",event);
		context.setNextUrl(path+"ProductStorageNowList.jsp"+"?isFirstVisit="+isFirstVisit);
	}
	private void backProductStorage(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"ProductStorageList.jsp");
	}
	
	//add by Wing start
    private HSSFCell createCell(HSSFWorkbook aWb, HSSFRow aRow, short column,String aValue){
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
        return tCell;
    }
    
    private void exportProductStorage(DBConnection con,SrvContext context,String path) throws Exception
    {
        HSSFWorkbook tWb = new HSSFWorkbook();
        HSSFSheet tSheet = tWb.createSheet();
        tWb.setSheetName(0,"库存统计",HSSFCell.ENCODING_UTF_16);
        HSSFRow tRow = tSheet.createRow((short)0);
        HSSFCell tCell0  = createCell(tWb,tRow,(short)0, "仓库");
        HSSFCell tCell1  = createCell(tWb,tRow,(short)1, "货位");
        HSSFCell tCell2  = createCell(tWb,tRow,(short)2, "产品编码");
        HSSFCell tCell3  = createCell(tWb,tRow,(short)3, "产品名称");
        HSSFCell tCell4  = createCell(tWb,tRow,(short)4, "单位");
        HSSFCell tCell5  = createCell(tWb,tRow,(short)5, "规格");
        HSSFCell tCell6  = createCell(tWb,tRow,(short)6, "型号");
        HSSFCell tCell7  = createCell(tWb,tRow,(short)7, "品牌");
        HSSFCell tCell8  = createCell(tWb,tRow,(short)8, "供应商");
        HSSFCell tCell9  = createCell(tWb,tRow,(short)9, "单价");
        HSSFCell tCell10  = createCell(tWb,tRow,(short)10, "数量");
        HSSFCell tCell11 = createCell(tWb,tRow,(short)11,"金额");
        HSSFCell tCell12 = createCell(tWb,tRow,(short)12,"结算模式");
        HSSFCell tCell13 = createCell(tWb,tRow,(short)13,"最后操作时间");
        HSSFCell tCell14 = createCell(tWb,tRow,(short)14,"管理员");
        HSSFCell tCell15 = createCell(tWb,tRow,(short)15,"图件号");
        if(isTel){
        	HSSFCell tCell16 = createCell(tWb,tRow,(short)16,"进价");
        }
        WarehouseProductViewSearchEvent event = (WarehouseProductViewSearchEvent)context.getSessionValue("WarehouseProductViewSearchEvent");
        event.setPageNum(1);
        event.setPageSize(ConstDefine.INT_MAX);
        ObjectList list = WarehouseProductView.fetch().search(con,event);
        //-----------------------
        String sql = "select sum(Product_Money) from WAREHOUSE_PRODUCT_VIEW ";
        DBResult dbrs = DBEngine.doSelect(con, sql, event.toConditions());
        ResultSet rs = dbrs.getRs();
        String allMoney = "0";
        if(rs.next()){
            allMoney = dbrs.getRs().getString(1)==null ? "0" : dbrs.getRs().getString(1);    
        }
        dbrs.clear();
        //------------------------
        for(int i=0;i<list.size();i++)
        {
            WarehouseProductView obj = (WarehouseProductView)list.get(i);
            ProductStorage ps = ProductStorage.fetch(con, obj.getId());
            if(ps == null) ps = ProductStorage.fetch();
            Product pro = Product.fetch(con,obj.getProductId());
 //           String[] str = ProductStorage.getSimpleProductStorage(obj.getProductId());
            if (pro == null) pro = new Product();
            HSSFRow row = tSheet.createRow((short)i+1);
            HSSFCell cell0  = createCell(tWb,row,(short)0, obj.getWarehouseName() == null ? "":obj.getWarehouseName());
            HSSFCell cell1  = createCell(tWb,row,(short)1, obj.getPositionName() == null ? "":obj.getPositionName());
            HSSFCell cell2  = createCell(tWb,row,(short)2, obj.getProductCode() == null ? "":obj.getProductCode());
            HSSFCell cell3  = createCell(tWb,row,(short)3, pro.getName() == null ? "":pro.getName());
            HSSFCell cell4  = createCell(tWb,row,(short)4, pro.getProductUnit() == null ? "":pro.getProductUnit());
            HSSFCell cell5  = createCell(tWb,row,(short)5, pro.getSpec() == null ? "":pro.getSpec());
            HSSFCell cell6  = createCell(tWb,row,(short)6, pro.getModel() == null ? "":pro.getModel());
            HSSFCell cell7  = createCell(tWb,row,(short)7, pro.getProductBrand() == null ? "":pro.getProductBrand());
            HSSFCell cell8  = createCell(tWb,row,(short)8, ps.viewProviderName(con));
            HSSFCell cell9  = createCell(tWb,row,(short)9, framework.core.StringUtil.showPrice(ps.viewProductPrice(con)));
            HSSFCell cell10  = createCell(tWb,row,(short)10, obj.viewProductCount(con));
            HSSFCell cell11 = createCell(tWb,row,(short)11,StringUtil.showPrice(obj.viewProductMoney(con)));
            HSSFCell cell12 = createCell(tWb,row,(short)12,obj.getBalanceMode().equals("1")?"压库":"买断");
            HSSFCell cell13 = createCell(tWb,row,(short)13,obj.viewLastUpdateTime(con));
            
         	GSAutoAuditongDateConfigurator.getInstance();
        	WarehouseStaff staff =null;
            String warehouseCode = obj.getWarehouseCode().substring(0,obj.getWarehouseCode().length()-2);
            if(GSAutoAuditongDateConfigurator.isShareDatesWarehouses(warehouseCode)){
            	Conditions conditions = new Conditions();
            	conditions.add("product_id",obj.getProductId());
            	if(GSAutoAuditongDateConfigurator.isNoShareWarehouseProductDatesWarehouses(warehouseCode)){
            		conditions.add("warehouse_code",conditions.LIKE,obj.getWarehouseCode()+"%");
            	}else{
            		conditions.add("warehouse_code",conditions.LIKE,warehouseCode+"%");
            	}
            	WarehouseProduct wp =WarehouseProduct.fetch(con,conditions);
            	if(wp==null){
            		staff=null;
            	}else{
            		staff = WarehouseStaff.fetch(con,wp.getKeeperStaffId());
            	}
            	
            } else{
                staff = WarehouseStaff.fetch(con,obj.getKeeperStaffId());
            }
            
            HSSFCell cell14 = createCell(tWb,row,(short)14,staff==null?"":staff.getName());
            HSSFCell cell15 = createCell(tWb,row,(short)15,pro.getDrawingPieceCode()== null ? "":pro.getDrawingPieceCode());
            if(isTel){
            	  HSSFCell cell16 = createCell(tWb,row,(short)16,ps.viewActualPrice(con));
            }
        }
        
        HSSFRow row = tSheet.createRow((short)list.size()+1);
        HSSFCell cell0  = createCell(tWb,row,(short)0, "");
        HSSFCell cell1  = createCell(tWb,row,(short)1, "");
        HSSFCell cell2  = createCell(tWb,row,(short)2, "");
        HSSFCell cell3  = createCell(tWb,row,(short)3, "");
        HSSFCell cell4  = createCell(tWb,row,(short)4, "");
        HSSFCell cell5  = createCell(tWb,row,(short)5, "");
        HSSFCell cell6  = createCell(tWb,row,(short)6, "");
        HSSFCell cell7  = createCell(tWb,row,(short)7, "");
        HSSFCell cell8  = createCell(tWb,row,(short)8, "");
        HSSFCell cell9  = createCell(tWb,row,(short)9, "");
        HSSFCell cell10 = createCell(tWb,row,(short)10, "");
        HSSFCell cell11 = createCell(tWb,row,(short)11, "");
        HSSFCell cell12 = createCell(tWb,row,(short)12, "");
        HSSFCell cell13 = createCell(tWb,row,(short)13, "当前条件总金额");
        HSSFCell cell14 = createCell(tWb,row,(short)14, allMoney);
        HSSFCell cell15 = createCell(tWb,row,(short)15, "");
        DateUtil date = new DateUtil();
        String year = date.getYear() + "";
        String month = date.getMonth() + "";
        if (date.getMonth() < 10) month = "0" + date.getMonth();
        String day = date.getDay() + "";
        if (date.getDay() < 10) day = "0" + date.getDay();
        String hour = date.getHour() + "";
        if (date.getHour() < 10) hour = "0" + date.getHour();
        String minute = date.getMinute() + "";
        if (date.getMinute() < 10) minute = "0" + date.getMinute();
        String second = date.getSecond() + "";
        if (date.getSecond() < 10) second = "0" + date.getSecond();
        
        String fileName = "STOCK_" + year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + second + ".xls";
        
        context.m_res.setContentType("application/ms-excel");
        context.m_res.addHeader("Content-disposition", "filename="+fileName);
        ServletOutputStream out = context.getServletOutputStream();
        tWb.write(out);
        out.flush();
        out.close();
        
        return ;
    }
    //add by Wing end
    
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		if(action_code.equalsIgnoreCase("listProductStorageNow")){
		    ObjectList warehouseList = Warehouse.fetchAllOrgs(con, loginContext.getLoginWarehouseCode());
		    context.setSessionValue("WarehouseListDetail", warehouseList);
		    listProductStorageNow(con,context,loginContext,path);
			return;
		}
		//根据指定的产品查看库存
		if(action_code.equalsIgnoreCase("listProductStorageShowNow")){
		    ObjectList warehouseList = Warehouse.fetchAllOrgs(con, loginContext.getLoginWarehouseCode());
		    context.setSessionValue("WarehouseListDetail", warehouseList);
		    listProductStorageShowNow(con,context,loginContext,path);
			return;
		}
		if(action_code.equalsIgnoreCase("listProductStorageShowNowQuery")){
		    ObjectList warehouseList = Warehouse.fetchAllOrgs(con, loginContext.getLoginWarehouseCode());
		    context.setSessionValue("WarehouseListDetail", warehouseList);
		    listProductStorageShowNowQuery(con,context,loginContext,path);
			return;
		}
		if(action_code.equalsIgnoreCase("listProductStorage"))
		{
		    //WarehouseSearchEvent wSearchEvent = new WarehouseSearchEvent();
		    //wSearchEvent.setWarehouseCodeUser(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode()));
		    //ObjectList warehouseList = Warehouse.fetch().search(con, wSearchEvent);
		    ObjectList warehouseList = Warehouse.fetchAllOrgs(con, loginContext.getLoginWarehouseCode());
		    context.setSessionValue("WarehouseListDetail", warehouseList);
		    listProductStorage(con,context,loginContext,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewProductStorage"))
		{
			String id = context.getString("Id",null);
			ProductStorage obj = ProductStorage.fetch(con,id);
			context.setValue("ProductStorage",obj);
			context.setNextUrl(path+"ProductStorageView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteProductStorage"))
		{
			context.setErrorUrl(path+"ProductStorageList.jsp");
			deleteProductStorage(con,context);
			backProductStorage(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddProductStorage"))
		{
			ProductStorage obj = ProductStorage.fetch();
			ProductStoragePageData data = (ProductStoragePageData)PageData.fromTable(obj,ProductStoragePageData.class);
			context.setValue("ProductStoragePageData",data);
			context.setNextUrl(path+"ProductStorageEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateProductStorage"))
		{
			String id = context.getString("Id",null);
			ProductStorage obj = ProductStorage.fetch(con,id);
			ProductStoragePageData data = (ProductStoragePageData)PageData.fromTable(obj,ProductStoragePageData.class);
			context.setValue("ProductStoragePageData",data);
			context.setNextUrl(path+"ProductStorageEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyProductStorage"))
		{
			String id = context.getString("Id",null);
			ProductStorage obj = ProductStorage.copy(ProductStorage.fetch(con,id));
			ProductStoragePageData data = (ProductStoragePageData)PageData.fromTable(obj,ProductStoragePageData.class);
			context.setValue("ProductStoragePageData",data);
			context.setNextUrl(path+"ProductStorageEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveProductStorage"))
		{
			context.setErrorUrl(path+"ProductStorageEdit.jsp");
			saveProductStorage(con,context,loginContext);
			backProductStorage(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewProductStorage"))
		{
			context.setErrorUrl(path+"ProductStorageEdit.jsp");
			saveProductStorage(con,context,loginContext);
			ProductStorage obj = ProductStorage.fetch();
			ProductStoragePageData data = (ProductStoragePageData)PageData.fromTable(obj,ProductStoragePageData.class);
			context.setValue("ProductStoragePageData",data);
			context.setNextUrl(path+"ProductStorageEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListProductStorage"))
		{
			backProductStorage(con,context,path);
			return;
		}
		
		//add by Wing start
		if(action_code.equalsIgnoreCase("exportProductStorage"))
        {
			LoginContext loginInfo = LoginContext.get(context);
			User user=loginInfo.getUser();
			isTel=user.getTel().equals("1");
            exportProductStorage(con,context,path);
            return;
        }
		
		if(action_code.equalsIgnoreCase("exportProductStorageNow"))
        {
            exportProductStorageNow(con,context,path);
            return;
        }
		if(action_code.equalsIgnoreCase("exportProductStorageShowNow"))
        {
            exportProductStorageShowNow(con,context,path);
            return;
        }
		//add by Wing end

		Logger.throwException("ProductStorageSrv","unknown action_code="+action_code);
	}
	 private void exportProductStorageNow(DBConnection con,SrvContext context,String path) throws Exception
	    {
	        HSSFWorkbook tWb = new HSSFWorkbook();
	        HSSFSheet tSheet = tWb.createSheet();
	        tWb.setSheetName(0,"库存统计",HSSFCell.ENCODING_UTF_16);
	        HSSFRow tRow = tSheet.createRow((short)0);
	        HSSFCell tCell0  = createCell(tWb,tRow,(short)0, "仓库");
	        HSSFCell tCell1  = createCell(tWb,tRow,(short)1, "货位");
	        HSSFCell tCell2  = createCell(tWb,tRow,(short)2, "产品编码");
	        HSSFCell tCell3  = createCell(tWb,tRow,(short)3, "产品名称");
	        HSSFCell tCell4  = createCell(tWb,tRow,(short)4, "单位");
	        HSSFCell tCell5  = createCell(tWb,tRow,(short)5, "规格");
	        HSSFCell tCell6  = createCell(tWb,tRow,(short)6, "型号");
	        HSSFCell tCell7  = createCell(tWb,tRow,(short)7, "品牌");
	        HSSFCell tCell8  = createCell(tWb,tRow,(short)8, "供应商");
	        HSSFCell tCell9  = createCell(tWb,tRow,(short)9, "单价");
	        HSSFCell tCell10  = createCell(tWb,tRow,(short)10, "数量");
	        HSSFCell tCell11 = createCell(tWb,tRow,(short)11,"金额");
	        HSSFCell tCell12 = createCell(tWb,tRow,(short)12,"结算模式");
	        HSSFCell tCell13 = createCell(tWb,tRow,(short)13,"最后操作时间");
	        HSSFCell tCell14 = createCell(tWb,tRow,(short)14,"管理员");
	        HSSFCell tCell15 = createCell(tWb,tRow,(short)15,"图件号");
	        
	        ProductStorageSearchEvent event = (ProductStorageSearchEvent)context.getSessionValue("WarehouseProductViewSearchEvent");
	        event.setPageNum(1);
	        event.setPageSize(ConstDefine.INT_MAX);
	        ObjectList list = ProductStorage.fetch().search(con,event);
	        //-----------------------
	        String sql = "select sum(Product_Money) from T_Product_Storage ";
	        DBResult dbrs = DBEngine.doSelect(con, sql, event.toConditions());
	        ResultSet rs = dbrs.getRs();
	        String allMoney = "0";
	        if(rs.next()){
	            allMoney = dbrs.getRs().getString(1)==null ? "0" : dbrs.getRs().getString(1);    
	        }
	        dbrs.clear();
	        //------------------------
	        for(int i=0;i<list.size();i++)
	        {
	        	ProductStorage obj = (ProductStorage)list.get(i);
	            ProductStorage ps = ProductStorage.fetch(con, obj.getId());
	            if(ps == null) ps = ProductStorage.fetch();
	            Product pro = Product.fetch(con,obj.getProductId());
	            //String[] str = ProductStorage.getSimpleProductStorage(obj.getProductId());
	            if (pro == null) pro = new Product();
	            HSSFRow row = tSheet.createRow((short)i+1);
	            HSSFCell cell0  = createCell(tWb,row,(short)0, obj.getWarehouseName() == null ? "":obj.getWarehouseName());
	            HSSFCell cell1  = createCell(tWb,row,(short)1, obj.getPositionName() == null ? "":obj.getPositionName());
	            HSSFCell cell2  = createCell(tWb,row,(short)2, obj.getProductCode() == null ? "":obj.getProductCode());
	            HSSFCell cell3  = createCell(tWb,row,(short)3, pro.getName() == null ? "":pro.getName());
	            HSSFCell cell4  = createCell(tWb,row,(short)4, pro.getProductUnit() == null ? "":pro.getProductUnit());
	            HSSFCell cell5  = createCell(tWb,row,(short)5, pro.getSpec() == null ? "":pro.getSpec());
	            HSSFCell cell6  = createCell(tWb,row,(short)6, pro.getModel() == null ? "":pro.getModel());
	            HSSFCell cell7  = createCell(tWb,row,(short)7, pro.getProductBrand() == null ? "":pro.getProductBrand());
	            HSSFCell cell8  = createCell(tWb,row,(short)8, ps.viewProviderName(con));
	            HSSFCell cell9  = createCell(tWb,row,(short)9, framework.core.StringUtil.showPrice(ps.viewProductPrice(con)));
	            HSSFCell cell10  = createCell(tWb,row,(short)10, obj.viewProductCount(con));
	            HSSFCell cell11 = createCell(tWb,row,(short)11,StringUtil.showPrice(obj.viewProductMoney(con)));
	            HSSFCell cell12 = createCell(tWb,row,(short)12,obj.getBalanceMode().equals("1")?"压库":"买断");
	            HSSFCell cell13 = createCell(tWb,row,(short)13,obj.viewLastUpdateTime(con));
	            Conditions warehouseConditions = new Conditions();
	        	warehouseConditions.add("warehouse_code",warehouseConditions.LIKE,obj.getWarehouseCode()+"%");
	        	warehouseConditions.add("product_id",obj.getProductId());
	        	WarehouseProduct warehousep = WarehouseProduct.fetch(con,warehouseConditions);
	        	Conditions warehouseStaffConditions = new Conditions();
	        	warehouseStaffConditions.add("keeper_staff_id",warehousep.getKeeperStaffId());
	            WarehouseStaff staff = WarehouseStaff.fetch(con,warehousep.getKeeperStaffId());
	            HSSFCell cell14 = createCell(tWb,row,(short)14,staff==null?"":staff.getName());
	            HSSFCell cell15 = createCell(tWb,row,(short)15,pro.getDrawingPieceCode()== null ? "":pro.getDrawingPieceCode());
	        }
	        
	        HSSFRow row = tSheet.createRow((short)list.size()+1);
	        HSSFCell cell0  = createCell(tWb,row,(short)0, "");
	        HSSFCell cell1  = createCell(tWb,row,(short)1, "");
	        HSSFCell cell2  = createCell(tWb,row,(short)2, "");
	        HSSFCell cell3  = createCell(tWb,row,(short)3, "");
	        HSSFCell cell4  = createCell(tWb,row,(short)4, "");
	        HSSFCell cell5  = createCell(tWb,row,(short)5, "");
	        HSSFCell cell6  = createCell(tWb,row,(short)6, "");
	        HSSFCell cell7  = createCell(tWb,row,(short)7, "");
	        HSSFCell cell8  = createCell(tWb,row,(short)8, "");
	        HSSFCell cell9  = createCell(tWb,row,(short)9, "");
	        HSSFCell cell10 = createCell(tWb,row,(short)10, "");
	        HSSFCell cell11 = createCell(tWb,row,(short)11, "");
	        HSSFCell cell12 = createCell(tWb,row,(short)12, "");
	        HSSFCell cell13 = createCell(tWb,row,(short)13, "当前条件总金额");
	        HSSFCell cell14 = createCell(tWb,row,(short)14, allMoney);
	        HSSFCell cell15 = createCell(tWb,row,(short)15, "");
	        DateUtil date = new DateUtil();
	        String year = date.getYear() + "";
	        String month = date.getMonth() + "";
	        if (date.getMonth() < 10) month = "0" + date.getMonth();
	        String day = date.getDay() + "";
	        if (date.getDay() < 10) day = "0" + date.getDay();
	        String hour = date.getHour() + "";
	        if (date.getHour() < 10) hour = "0" + date.getHour();
	        String minute = date.getMinute() + "";
	        if (date.getMinute() < 10) minute = "0" + date.getMinute();
	        String second = date.getSecond() + "";
	        if (date.getSecond() < 10) second = "0" + date.getSecond();
	        
	        String fileName = "STOCK_" + year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + second + ".xls";
	        
	        context.m_res.setContentType("application/ms-excel");
	        context.m_res.addHeader("Content-disposition", "filename="+fileName);
	        ServletOutputStream out = context.getServletOutputStream();
	        tWb.write(out);
	        out.flush();
	        out.close();
	        
	        return ;
	    }
	 private void exportProductStorageShowNow(DBConnection con,SrvContext context,String path) throws Exception
	    {
	        HSSFWorkbook tWb = new HSSFWorkbook();
	        HSSFSheet tSheet = tWb.createSheet();
	        tWb.setSheetName(0,"库存统计",HSSFCell.ENCODING_UTF_16);
	        HSSFRow tRow = tSheet.createRow((short)0);
	        HSSFCell tCell0  = createCell(tWb,tRow,(short)0, "仓库");
	        HSSFCell tCell1  = createCell(tWb,tRow,(short)1, "货位");
	        HSSFCell tCell2  = createCell(tWb,tRow,(short)2, "产品编码");
	        HSSFCell tCell3  = createCell(tWb,tRow,(short)3, "产品名称");
	        HSSFCell tCell4  = createCell(tWb,tRow,(short)4, "单位");
	        HSSFCell tCell5  = createCell(tWb,tRow,(short)5, "规格");
	        HSSFCell tCell6  = createCell(tWb,tRow,(short)6, "型号");
	        HSSFCell tCell7  = createCell(tWb,tRow,(short)7, "品牌");
	        HSSFCell tCell8  = createCell(tWb,tRow,(short)8, "供应商");
	        HSSFCell tCell9  = createCell(tWb,tRow,(short)9, "单价");
	        HSSFCell tCell10  = createCell(tWb,tRow,(short)10, "数量");
	        HSSFCell tCell11 = createCell(tWb,tRow,(short)11,"金额");
	        HSSFCell tCell12 = createCell(tWb,tRow,(short)12,"结算模式");
	        HSSFCell tCell13 = createCell(tWb,tRow,(short)13,"最后操作时间");
	        HSSFCell tCell15 = createCell(tWb,tRow,(short)14,"图件号");
	        
	        ProductStorageSearchEvent event = (ProductStorageSearchEvent)context.getSessionValue("WarehouseProductViewSearchEvent");
	        event.setIsShow("1");
	        event.setPageNum(1);
	        event.setPageSize(ConstDefine.INT_MAX);
	        ObjectList list = ProductStorage.fetch().search(con,event);
	        //-----------------------
	        String sql = "select sum(Product_Money) from T_Product_Storage ";
	        DBResult dbrs = DBEngine.doSelect(con, sql, event.toConditions());
	        ResultSet rs = dbrs.getRs();
	        String allMoney = "0";
	        if(rs.next()){
	            allMoney = dbrs.getRs().getString(1)==null ? "0" : dbrs.getRs().getString(1);    
	        }
	        dbrs.clear();
	        //------------------------
	        for(int i=0;i<list.size();i++)
	        {
	        	ProductStorage obj = (ProductStorage)list.get(i);
	            ProductStorage ps = ProductStorage.fetch(con, obj.getId());
	            if(ps == null) ps = ProductStorage.fetch();
	            Product pro = Product.fetch(con,obj.getProductId());
	            //String[] str = ProductStorage.getSimpleProductStorage(obj.getProductId());
	            if (pro == null) pro = new Product();
	            HSSFRow row = tSheet.createRow((short)i+1);
	            HSSFCell cell0  = createCell(tWb,row,(short)0, obj.getWarehouseName() == null ? "":obj.getWarehouseName());
	            HSSFCell cell1  = createCell(tWb,row,(short)1, obj.getPositionName() == null ? "":obj.getPositionName());
	            HSSFCell cell2  = createCell(tWb,row,(short)2, obj.getProductCode() == null ? "":obj.getProductCode());
	            HSSFCell cell3  = createCell(tWb,row,(short)3, pro.getName() == null ? "":pro.getName());
	            HSSFCell cell4  = createCell(tWb,row,(short)4, pro.getProductUnit() == null ? "":pro.getProductUnit());
	            HSSFCell cell5  = createCell(tWb,row,(short)5, pro.getSpec() == null ? "":pro.getSpec());
	            HSSFCell cell6  = createCell(tWb,row,(short)6, pro.getModel() == null ? "":pro.getModel());
	            HSSFCell cell7  = createCell(tWb,row,(short)7, pro.getProductBrand() == null ? "":pro.getProductBrand());
	            HSSFCell cell8  = createCell(tWb,row,(short)8, ps.viewProviderName(con));
	            HSSFCell cell9  = createCell(tWb,row,(short)9, framework.core.StringUtil.showPrice(ps.viewProductPrice(con)));
	            HSSFCell cell10  = createCell(tWb,row,(short)10, obj.viewProductCount(con));
	            HSSFCell cell11 = createCell(tWb,row,(short)11,StringUtil.showPrice(obj.viewProductMoney(con)));
	            HSSFCell cell12 = createCell(tWb,row,(short)12,obj.getBalanceMode().equals("1")?"压库":"买断");
	            HSSFCell cell13 = createCell(tWb,row,(short)13,obj.viewLastUpdateTime(con));
//	            Conditions warehouseConditions = new Conditions();
//	        	warehouseConditions.add("warehouse_code",warehouseConditions.LIKE,obj.getWarehouseCode()+"%");
//	        	warehouseConditions.add("product_id",obj.getProductId());
//	        	WarehouseProduct warehousep = WarehouseProduct.fetch(con,warehouseConditions);
//	        	Conditions warehouseStaffConditions = new Conditions();
//	        	warehouseStaffConditions.add("keeper_staff_id",warehousep.getKeeperStaffId());
//	            WarehouseStaff staff = WarehouseStaff.fetch(con,warehousep.getKeeperStaffId());
//	            HSSFCell cell14 = createCell(tWb,row,(short)14,staff==null?"":staff.getName());
	            HSSFCell cell14 = createCell(tWb,row,(short)14,pro.getDrawingPieceCode()== null ? "":pro.getDrawingPieceCode());
	        }
	        
	        HSSFRow row = tSheet.createRow((short)list.size()+1);
	        HSSFCell cell0  = createCell(tWb,row,(short)0, "");
	        HSSFCell cell1  = createCell(tWb,row,(short)1, "");
	        HSSFCell cell2  = createCell(tWb,row,(short)2, "");
	        HSSFCell cell3  = createCell(tWb,row,(short)3, "");
	        HSSFCell cell4  = createCell(tWb,row,(short)4, "");
	        HSSFCell cell5  = createCell(tWb,row,(short)5, "");
	        HSSFCell cell6  = createCell(tWb,row,(short)6, "");
	        HSSFCell cell7  = createCell(tWb,row,(short)7, "");
	        HSSFCell cell8  = createCell(tWb,row,(short)8, "");
	        HSSFCell cell9  = createCell(tWb,row,(short)9, "");
	        HSSFCell cell10 = createCell(tWb,row,(short)10, "");
	        HSSFCell cell11 = createCell(tWb,row,(short)11, "");
	        HSSFCell cell12 = createCell(tWb,row,(short)12, "");
	        HSSFCell cell13 = createCell(tWb,row,(short)13, "当前条件总金额");
	        HSSFCell cell14 = createCell(tWb,row,(short)14, allMoney);
	        HSSFCell cell15 = createCell(tWb,row,(short)15, "");
	        DateUtil date = new DateUtil();
	        String year = date.getYear() + "";
	        String month = date.getMonth() + "";
	        if (date.getMonth() < 10) month = "0" + date.getMonth();
	        String day = date.getDay() + "";
	        if (date.getDay() < 10) day = "0" + date.getDay();
	        String hour = date.getHour() + "";
	        if (date.getHour() < 10) hour = "0" + date.getHour();
	        String minute = date.getMinute() + "";
	        if (date.getMinute() < 10) minute = "0" + date.getMinute();
	        String second = date.getSecond() + "";
	        if (date.getSecond() < 10) second = "0" + date.getSecond();
	        
	        String fileName = "STOCK_" + year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + second + ".xls";
	        
	        context.m_res.setContentType("application/ms-excel");
	        context.m_res.addHeader("Content-disposition", "filename="+fileName);
	        ServletOutputStream out = context.getServletOutputStream();
	        tWb.write(out);
	        out.flush();
	        out.close();
	        
	        return ;
	    }

}
